#include "../../inc/protocol.h"

struct DictOLPDU *CreateRegisterREQPDU(char *name,char *password, int type)
{
    struct DictOLPDU* pdu = NULL;
    int len = 0;
    len = 2*sizeof(int)+NAME_LEN+PASS_LEN;
    pdu = (struct DictOLPDU*)malloc(len);
    if(NULL == pdu)
    {
	printf("malloc for Register_REQ failed\n");
	return NULL;
    }
    memset(pdu,0,len);
    pdu->len = len;
    pdu->type = REG_REQ;
    {
	strncpy(pdu->buf,name,NAME_LEN-1);
	strncpy(pdu->buf+NAME_LEN,password,PASS_LEN-1);
    }
    return pdu;
}

struct DictOLPDU *CreateRegisterRSPPDU(int value)
{
    int len = 0;
    struct DictOLPDU* pdu = NULL;
    len = 2*sizeof(int) + sizeof(int);
    pdu = (struct DictOLPDU*)malloc(len);
    if(NULL == pdu)
    {
	printf("malloc for Create_Register_RSP failed\n");
	return NULL;
    }
    memset(pdu,0,len);
    pdu->len = len;
    pdu->type = REG_RSP;
    sprintf(pdu->buf,"%d",value);
    return pdu;
}

struct DictOLPDU *CreateLoginREQPDU(char *name,char *password)
{
    struct DictOLPDU* pdu = NULL;
    int len = 0;
    len = 2*sizeof(int) + NAME_LEN+PASS_LEN;
    pdu = (struct DictOLPDU*)malloc(len);
    if(NULL == pdu)
    {
	printf("malloc for Register_REQ failed\n");
	return NULL;
    }
    memset(pdu,0,len);
    pdu ->len = len;
    pdu->type = LOGIN_REQ;
    {
	strncpy(pdu->buf,name,NAME_LEN-1);
	strncpy(pdu->buf+NAME_LEN,password,PASS_LEN-1);
    }
    return pdu;
}

struct DictOLPDU *CreateLoginRSPPDU(int value)
{
    int len = 0;
    struct DictOLPDU* pdu = NULL;
    len = 2*sizeof(int) + sizeof(int);
    pdu = (struct DictOLPDU*)malloc(len);
    if(NULL == pdu)
    {
	printf("malloc for Create_Register_RSP failed\n");
	return NULL;
    }
    memset(pdu,0,len);
    pdu->len = len;
    pdu->type = LOGIN_RSP;
    //sprintf(pdu->buf,"%d",value);
    *(int*)(pdu->buf) = value;
    return pdu;
    
}
struct DictOLPDU *CreateQueryWordREQPDU(char *word)
{
    int len = 0;
    struct DictOLPDU* pdu = NULL;
    len = 2*sizeof(int) + sizeof(word)+1;
    pdu = (struct DictOLPDU*)malloc(len);
    if(NULL == pdu)
    {
	printf("malloc for Create_Register_RSP failed\n");
	return NULL;
    }
    memset(pdu,0,len);
    pdu->len = len;
    pdu->type = QUERYWORD_REQ;
    strcpy(pdu->buf,word);
    return pdu;
}


struct DictOLPDU *CreateQueryWordRSPPDU(char *explain) 
{
    int len = 0;
    struct DictOLPDU* pdu = NULL;
    len = 2*sizeof(int) + strlen(explain)+1;
    pdu = (struct DictOLPDU*)malloc(len);
    if(NULL == pdu)
    {
	printf("malloc for Create_Register_RSP failed\n");
	return NULL;
    }
    memset(pdu,0,len);
    pdu->len = len;
    pdu->type = QUERYWORD_RSP;
    strcpy(pdu->buf,explain);
    return pdu;	
}

struct DictOLPDU *CreateHistoryREQPDU()
{
    int len = 0;
    struct DictOLPDU* pdu = NULL;
    len = 2*sizeof(int) ;
    pdu = (struct DictOLPDU*)malloc(len);
    if(NULL == pdu)
    {
	printf("malloc for Create_Register_RSP failed\n");
	return NULL;
    }
    memset(pdu,0,len);
    pdu->len = len;
    pdu->type = HISTORY_REQ;
    return pdu;

}

struct DictOLPDU *CreateHistoryRSPPDU(char *history) 
{
    int len = 0;
    struct DictOLPDU* pdu = NULL;
    len = 2*sizeof(int) +strlen(history)+1;
    pdu = (struct DictOLPDU*)malloc(len);
    if(NULL == pdu)
    {
	printf("malloc for Create_Register_RSP failed\n");
	return NULL;
    }
    memset(pdu,0,len);
    pdu->len = len;
    pdu->type =HISTORY_RSP;
    strcpy(pdu->buf,history);
    return pdu;
}    
struct DictOLPDU * RecvPDU(int fd)
{
	int len = 0;
	struct  DictOLPDU  *pdu = NULL;
	int ret = 0;

	/*1. 接收sizeof(int)个字节的总长度到变量len的空间里*/
	ret = read(fd,&len,sizeof(int));
	if(ret != sizeof(int))
	{
		printf("recv pdu len failed\n");
		return NULL;
	}
	
	/*2. 动态分配len个字节的内存空间*/
	pdu = (struct  DictOLPDU *)malloc(len);
	if(NULL == pdu)
	{
		printf("malloc for recv pdu failed\n");
		return NULL;
	}
	memset(pdu,0,len);


	/*3. 成员len赋值为len*/
	pdu->len = len;

	/*4. 接收len-sizeof(int)字节的后续数据*/
	ret = read(fd,&pdu->type,len - sizeof(int));
	if(ret != len - sizeof(int))
	{
		printf("recv continue-data failed\n");
		free(pdu);
		pdu = NULL;
		return NULL;
	}

	/*5. 返回动态空间的首地址*/
	return pdu;
}
int DestroyDictOLPDU(struct DictOLPDU * pstPDU)//RecvPDU函数和所有的Create函数均有动态分配，本函数负责释放
{
	if(pstPDU != NULL)
	{
		free(pstPDU);
		pstPDU = NULL;
	}
	return 1;
}
int SendPDU(int fd,struct DictOLPDU *pstPDU)
{
	int ret = 0;

	ret = write(fd,pstPDU,pstPDU->len);
	if(ret != pstPDU->len)
	{
		printf("send pdu failed\n");
		return -1;
	}

	return 0;
}



